package com.atguigu.webflux.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.*;
import org.springframework.web.reactive.resource.PathResourceResolver;
import org.thymeleaf.spring6.view.reactive.ThymeleafReactiveViewResolver;

@Configuration
public class WebFluxConfig implements WebFluxConfigurer {
    //引入spring-boot-starter-thymeleaf自动会注入该bean
    @Autowired
    private ThymeleafReactiveViewResolver thymeleafReactiveViewResolver;


    /**
     * 加入thymeleaf试图解析器，不然找不到view name
     *
     * @param registry
     */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.viewResolver(thymeleafReactiveViewResolver);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowedOrigins("localhost");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");

        registry.addResourceHandler("/WebTours/**")
                .addResourceLocations("classpath:/static/WebTours/")
                .resourceChain(true)
                .addResolver(new PathResourceResolver());
    }
}